package com.crm.consult.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.crm.model.ConsultInfoManage;
import com.crm.model.ConsultInfoManageIllegal;
import com.crm.model.SysUser;
import com.crm.model.entity.ConsultInfoValueKey;
import com.crm.service.ConsultInfoManageIllegalService;
import com.crm.service.ConsultInfoManageService;
import com.crm.util.ExcelImportUtil;
import com.crm.util.ResponseUtil;
import com.crm.util.SessionModel;
import com.crm.util.SessionUtils;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("/import")
public class ImportController {
	private static final int limit = 200;
	@Autowired
	private ConsultInfoManageService  infoManageService;
	@Autowired
	private ConsultInfoManageIllegalService  illegalService;
	
	@RequestMapping("/index")
	public String index(HttpServletRequest request){
		SysUser sy = (SysUser)SessionUtils.get(SessionModel.SESSION_USER);
		if(sy==null){ 
			return "tologin";    
		}
		return "consultcenter/consultImport";
	}
	

	@RequestMapping("/consultInfo")
	@ResponseBody
	@SuppressWarnings("unchecked")
	public Map<String, Object> consultImport(HttpServletRequest request, HttpServletResponse response, String checkFile,
			@RequestParam("uploadFile") MultipartFile uploadFile) throws Exception {
		Map<String, Object> resultMap = null;
		String directoryPath = request.getSession().getServletContext().getRealPath(ExcelImportUtil.consultLoction);
		Map<String, String> headMap = ConsultInfoValueKey.consultHeadtMap;
		File excelFile = createExcelFile(directoryPath, uploadFile);
		if ("true".equals(checkFile)) {
			resultMap = ExcelImportUtil.checkImprotExcelData(excelFile, headMap);
		} else {
			List<Map<String,Object>> mapList = ExcelImportUtil.Excel2Map(excelFile,headMap);
			List<ConsultInfoManageIllegal> infoIllegalList=(List<ConsultInfoManageIllegal>)ExcelImportUtil.map2Object( mapList,ConsultInfoManageIllegal.class);
			if(infoIllegalList.size()<=limit){
				resultMap = illegalService.batchConsultInfoEnterFromExcel(infoIllegalList);
			}else{
				resultMap = illegalService.batchConsultInfoEnterFromExcelThroughThreadPool(infoIllegalList);
			}
		}
		excelFile.delete();
		return resultMap;
	}
	
	@RequestMapping("/display")
	public String display(HttpServletRequest request){
		SysUser sy = (SysUser)SessionUtils.get(SessionModel.SESSION_USER);
		if(sy==null){ 
			return "tologin";    
		}
		return "consultcenter/consultinfoIllegal";
	}
	
	@RequestMapping("/selectAllByWhere")
	public void selectAllByWhere(HttpServletResponse response,HttpServletRequest request, Page<ConsultInfoManageIllegal> page, ConsultInfoManageIllegal t){
		Page<ConsultInfoManageIllegal> list = (Page<ConsultInfoManageIllegal>) illegalService.selectAll(page, t);
		ResponseUtil.resultPage(response,request,list);
	}
	
	@RequestMapping("/updateRecord")
	@ResponseBody
	public Map<String,Object> updateRecord(HttpServletResponse response,HttpServletRequest request, ConsultInfoManage record){
		Map<String,Object> retMap  = new HashMap<String, Object>();
		String status=illegalService.insertConsultAnddelIllegalINfo(record);
		if("failure".equals(status)){
			retMap.put("msg", "已创建过该咨询量");
		}
		retMap.put("status", status);
		return retMap;
	}
	/**
	 * 保存信息到
	 * @param response
	 * @param request
	 * @param record
	 * @return
	 */
	@RequestMapping("/transfer")
	public Map<String,Object> transfer(HttpServletResponse response,HttpServletRequest request, ConsultInfoManageIllegal record){
		Map<String,Object> retMap  = new HashMap<String, Object>();
		String status =illegalService.transfer(record);
		retMap.put("status", status);
		return retMap;
	}

	/**
	 * 将上传文件保存在目录中
	 * 
	 * @param directoryPath
	 * @param uploadFile
	 * @return
	 * @throws IOException
	 */
	private static File createExcelFile(String directoryPath, MultipartFile uploadFile) throws IOException {
		String orifilename = uploadFile.getOriginalFilename();
		String suffixName = orifilename.substring(orifilename.lastIndexOf("."));
		File dirctoryFile = new File(directoryPath);
		if (!dirctoryFile.exists()) {
			dirctoryFile.mkdirs();
		}
		String filePath = directoryPath + File.separator + orifilename.substring(0, orifilename.lastIndexOf(".")) + "-"
				+ UUID.randomUUID().toString() + suffixName;
		File excelFile = new File(filePath);
		if (!excelFile.exists()) {
			excelFile.createNewFile();
		}
		uploadFile.transferTo(excelFile);
		return excelFile;
	}

}
